﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Match : MonoBehaviour
{
    public static Match instance;
    private GameManager manager;
    private List<GameObject> particle_list;
    private GameObject particle;
    private GameObject matched_particle;
    void Awake()
    {
        instance = this;
    }
    void Start()
    {
        manager = GameManager.getInstance();
        particle = new GameObject("Particle");
        particle_list = new List<GameObject>();
    }
    private GameObject tmp;
    public void SetMatchedCandy(List<GameObject> list, GameObject keep_candy, string type, bool neighbor_need_check)
    {
        tmp = null;
        SoundCon.instance.PlaySound(3);
        foreach (GameObject c in list)
        {
			c.GetComponent<CandyController>().IsMatched = true;
			c.GetComponent<CandyController>().check = !neighbor_need_check;
        }
        if (keep_candy != null)
        {
            if (tmp != null)
            {
				tmp.GetComponent<CandyController>().IsMatched = false;
                ChangeType(tmp, type);
                tmp = null;
                return;
            }
			keep_candy.GetComponent<CandyController>().IsMatched = false;
            ChangeType(keep_candy, type);
            tmp = null;
        }
//        Invoke("DestroyParticle", 1f);
        switch (type)
        {
            case "simple": SoundCon.instance.PlaySound(3); break;
            case "vertical": SoundCon.instance.PlaySound(4); break;
            case "horizontal": SoundCon.instance.PlaySound(4); break;
            case "special": SoundCon.instance.PlaySound(5); break;
            case "star": SoundCon.instance.PlaySound(6); break;
        }
    }
    public void ChangeType(GameObject obj, string type)
    {
		obj.GetComponent<CandyController>().Type = type;
		Destroy(obj.GetComponent<CandyController>().CandyObject);
        if (type == "star")
        {
			obj.GetComponent<CandyController>().Candy_Color = "star";
        }
		obj.GetComponent<CandyController>().CandyObject = CandyDemo.instance.candy_factory.getCandy(obj.GetComponent<CandyController>().Candy_Color, type).GetCandy();
		obj.GetComponent<CandyController>().CandyObject.transform.parent = obj.GetComponent<CandyController>().CandyHolder.transform;
		obj.GetComponent<CandyController>().CandyObject.transform.localPosition = Vector2.zero;
    }
//    private void DestroyParticle()
//    {
//        for (int i = 0; i < particle_list.Count; i++)
//        { Destroy(particle_list[i]); }
//        particle_list.Clear();
//    }
    public bool CreateSlideCandy(GameObject obj, GameObject objR, GameObject objL,
        GameObject objU, GameObject objD)
    {
		objR = obj.GetComponent<CandyController>().GetNeighbor("right");
		objL = obj.GetComponent<CandyController>().GetNeighbor("left");
		objU = obj.GetComponent<CandyController>().GetNeighbor("up");
		objD = obj.GetComponent<CandyController>().GetNeighbor("down");
		CandyEffect.instance.CreateEffect(objR, obj.GetComponent<CandyController>().Type);
		CandyEffect.instance.CreateEffect(objL, obj.GetComponent<CandyController>().Type);
		CandyEffect.instance.CreateEffect(objU, obj.GetComponent<CandyController>().Type);
		CandyEffect.instance.CreateEffect(objD, obj.GetComponent<CandyController>().Type);
	

		SlideRLUD(objR, "vertical");
		SlideRLUD(objL, "vertical");
		SlideRLUD(objU, "horizontal");
		SlideRLUD(objD, "horizontal");
        manager.IsMatch = true;
        return true;
    }
	public bool	 SlideRLUD(GameObject obj, string dir)
	{
		if(obj==null){return	false ;}
		obj.GetComponent<CandyController>().enabled=true;
		switch(dir){
		case "vertical":for(int i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
				CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().enabled=true;
				if(CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().XCoord==obj.GetComponent<CandyController>().XCoord){
					CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().IsMatched=true;
					CandyDemo.instance.UIscore.AddScore(20);
				}
			}
			break;
		case "horizontal":for(int i=0;i<CandyDemo.instance.level.GetCandy().Length;i++){
				CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().enabled=true;
				if(CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().YCoord==obj.GetComponent<CandyController>().YCoord){
					CandyDemo.instance.level.GetCandy()[i].GetComponent<CandyController>().IsMatched=true;
					CandyDemo.instance.UIscore.AddScore(20);
				}
			}
			break;
		}
		return false;
	}
    public bool CreateSlideCandy(GameObject obj, string dir)
    {
        SoundCon.instance.PlaySound(7);
        switch (dir)
        {
		case "vertical":SlideRLUD(obj, "vertical");
                break;
		case "horizontal": SlideRLUD(obj, "horizontal");
                break;

        }
        manager.IsMatch = true;
        return true;
    }
    private string[] candy_color = { "red", "green", "purple", "blue", "yellow", "orange" };
    private string color_;
    public void AttachWithStar(GameObject obj)
    {
        color_ = candy_color[Random.Range(0, candy_color.Length)];
        foreach (GameObject cs in CandyDemo.instance.level.GetCandy())
        {
			if (cs.GetComponent<CandyController>().Candy_Color == color_)
            {
				cs.GetComponent<CandyController>().IsMatched = true;
                CandyEffect.instance.candy_electric.Add(cs);
                CandyDemo.instance.UIscore.AddScore(20);
            }
        }
		manager.gamestate=GameState.destroy;
        CandyEffect.instance.AssignBurstUniversalObject(obj.transform.position, false);
        manager.IsMatch = true;
        SoundCon.instance.PlaySound(9);
    }
}
